//
//  TipsHud.m
//  MallComponents
//
//  Created by holyjoy on 15/8/11.
//  Copyright (c) 2015年 holyjoy. All rights reserved.
//

#import "TipsHud.h"
#import "MBProgressHUD.h"
#define WK(weakSelf) __weak __typeof(&*self)weakSelf = self;

@interface TipsHud()
@property (nonatomic, strong) MBProgressHUD * hud;
@end

@implementation TipsHud

+ (TipsHud *)sharedInstance{
    static TipsHud * tipsHud = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tipsHud = [[TipsHud alloc] init];
    });
    return tipsHud;
}

- (void)showLoadingOnView:(UIView *)view{
    if (_hud) {
        [_hud removeFromSuperview];
        _hud = nil;
    }
    WK(weakSelf)
//    dispatch_async(dispatch_get_main_queue(), ^{
//        weakSelf.hud = [[MBProgressHUD alloc] initWithView:view];
//        //    _hud.userInteractionEnabled = NO;//设置该属性是允许触摸 ！！！！
//        weakSelf.hud.label.text = @"加载中...";
//
//        [weakSelf.hud showAnimated:YES];
//
//
//        [view addSubview:weakSelf.hud];
//    });

    //此处有个小陷阱 异步线程中改变主线程对象 连续多次调用可能导致后面调起该方法时还未执行dispatch_async中的初始化操作 导致_hud为空 下一次执行该方法时前一次的_hud没有被移除
    _hud = [[MBProgressHUD alloc] initWithView:view];
    //    _hud.userInteractionEnabled = NO;//设置该属性是允许触摸 ！！！！
    _hud.label.text = @"加载中...";

    [_hud showAnimated:YES];
    
    //仅在主线程中更新视图即可
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [view addSubview:weakSelf.hud];
    });
    
}

- (void)showTips:(NSString *)str{
    [_hud removeFromSuperview];
    _hud = nil;
    
    UIWindow * keyWindow = [UIApplication sharedApplication].keyWindow;
//    _hud = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
    _hud = [[MBProgressHUD alloc] init];
    
    _hud.mode = MBProgressHUDModeCustomView;
    _hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    _hud.bezelView.backgroundColor = [UIColor blackColor];
    
    UILabel *label = [[UILabel alloc]init];
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize:15.0];
    label.textColor = [UIColor whiteColor];
    if (str.length > 0) {
        label.text = str;
        CGSize size =  [self sizeWithString:label.text font:label.font];
        label.frame = CGRectMake(0, 0, size.width, size.height);
        label.textAlignment = NSTextAlignmentCenter;
        _hud.customView = label;
        
        [keyWindow addSubview:_hud];
        
        [_hud showAnimated:YES];
//        [self hideTips:[self displayDurationForString:label.text]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)([self displayDurationForString:label.text] * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self hideTips];
        });
        
    }else{
        [_hud hideAnimated:YES];
    }

}

-(void)hideTips{
    _hud.removeFromSuperViewOnHide = YES;
    [_hud hideAnimated:YES];
    _hud = nil;
}

-(void)hideTips:(NSTimeInterval)delay{
    _hud.removeFromSuperViewOnHide = YES;
    [_hud hideAnimated:YES afterDelay:delay];
    _hud = nil;
}

- (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font
{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(200, 8000)//限制最大的宽度和高度
                                       options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading  |NSStringDrawingUsesLineFragmentOrigin//采用换行模式
                                    attributes:@{NSFontAttributeName: font}//传人的字体字典
                                       context:nil];
    
    return rect.size;
}

- (NSTimeInterval)displayDurationForString:(NSString*)string {
    return MIN((float)string.length*0.06 + 1, 3.0);
}

@end
